package lily.phs.object;

import lily.phs.database.Constants;
import lily.phs.urh.R;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;
import android.view.View.OnClickListener;

public class HereTipa implements Constants{
	private static final String[] PROJECTION = new String[] { 
		HETI_HERE_ID,HETI_NAME
	};
	
	private static final String[] PROJECTION_ = new String[] { 
		HETI_HERE_ID,HETI_NAME
	};
	
	private Context myContext;
	private int heti_profileid=-1;//内部适用于的档案ID，用于数据库各个表之间的链接
	private String heti_here_id="";//显示的档案ID
	/*提交到服务器上之后，两者一致:
	 * heti_here_id(18)=area_code(12)+heti_profileid(6)
	 * */
	private String heti_name="";
	private String heti_now_addr="";
	private String heti_hore_addr="";
	private String heti_tel="";
	private String heti_town_name="";
	private String heti_vico_name="";
	private String heti_buil_unit="";
	private String heti_buil_per="";
	private String heti_doctor="";
	private String heti_buil_date="";
	
	private String group;
	
	public HereTipa(Context context){
		this.myContext=context;
	}
	
	public int getHeti_profileid() {
		return heti_profileid;
	}
	public void setHeti_profileid(int hetiProfileid) {
		heti_profileid = hetiProfileid;
	}
	public String getHeti_here_id() {
		return heti_here_id;
	}
	public void setHeti_here_id(String hetiHereId) {
		heti_here_id = hetiHereId;
	}
	public String getHeti_name() {
		return heti_name;
	}
	public void setHeti_name(String hetiName) {
		heti_name = hetiName;
	}
	public String getHeti_now_addr() {
		return heti_now_addr;
	}
	public void setHeti_now_addr(String hetiNowAddr) {
		heti_now_addr = hetiNowAddr;
	}
	public String getHeti_tel() {
		return heti_tel;
	}
	public void setHeti_tel(String hetiTel) {
		heti_tel = hetiTel;
	}
	public String getHeti_town_name() {
		return heti_town_name;
	}
	public void setHeti_town_name(String hetiTownName) {
		heti_town_name = hetiTownName;
	}
	public String getHeti_vico_name() {
		return heti_vico_name;
	}
	public void setHeti_vico_name(String hetiVicoName) {
		heti_vico_name = hetiVicoName;
	}
	public String getHeti_buil_unit() {
		return heti_buil_unit;
	}
	public void setHeti_buil_unit(String hetiBuilUnit) {
		heti_buil_unit = hetiBuilUnit;
	}
	public String getHeti_buil_per() {
		return heti_buil_per;
	}
	public void setHeti_buil_per(String hetiBuilPer) {
		heti_buil_per = hetiBuilPer;
	}
	public String getHeti_buil_date() {
		return heti_buil_date;
	}
	public void setHeti_buil_date(String hetiBuilDate) {
		heti_buil_date = hetiBuilDate;
	}
	
	public String getHeti_doctor() {
		return heti_doctor;
	}

	public void setHeti_doctor(String hetiDoctor) {
		heti_doctor = hetiDoctor;
	}

	public String getHeti_hore_addr() {
		return heti_hore_addr;
	}

	public void setHeti_hore_addr(String hetiHoreAddr) {
		heti_hore_addr = hetiHoreAddr;
	}
	
	/**
	 * @return 现住址组号，位于“村”的下一级
	 */
	public String getAddressGroup() {
		String group = "";
		
		String p = myContext.getResources().getString(R.string.def_province);
		String c = myContext.getResources().getString(R.string.def_city);
		String ct = myContext.getResources().getString(R.string.def_county);
		String r = myContext.getResources().getString(R.string.def_rural);
		String v = myContext.getSharedPreferences(USERINFO, Context.MODE_PRIVATE).getString("def_village", "");
		if(heti_now_addr.length()>0){
			group = heti_now_addr.replace(p, "")
			.replace(c, "")
			.replace(ct, "")
			.replace(r, "")
			.replace(v, "");
			group = group.replace("组", "");
			if(group.length()<2){
				group = "0"+group;
			}
		}
		return group;
	}
	
	/**
	 * @return 户籍住址组号，位于“村”的下一级
	 */
	public String getHhGroup() {
		String group = "";
		String p = myContext.getResources().getString(R.string.def_province);
		String c = myContext.getResources().getString(R.string.def_city);
		String ct = myContext.getResources().getString(R.string.def_county);
		String r = myContext.getResources().getString(R.string.def_rural);
		String v = myContext.getSharedPreferences(USERINFO, Context.MODE_PRIVATE).getString("def_village", "");
		Log.i("upload", "def_village="+v);
		if(heti_hore_addr.length()>0){
			group = heti_hore_addr.replace(p, "")
			.replace(c, "")
			.replace(ct, "")
			.replace(r, "")
			.replace(v, "");
			group = group.replace("组", "");
			if(group.length()<2){
				group = "0"+group;
			}
		}
		return group;
	}

	public void insertHereTipa(){
		ContentValues value;
		value = new ContentValues();
		value.put(HETI_PROFILEID, this.getHeti_profileid());
		value.put(HETI_HERE_ID, this.getHeti_here_id());
		value.put(HETI_NAME, this.getHeti_name());
		value.put(HETI_NOW_ADDR, this.getHeti_now_addr());
		value.put(HETI_HORE_ADDR, this.getHeti_hore_addr());
		value.put(HETI_TEL, this.getHeti_tel());
		value.put(HETI_TOWN_NAME, this.getHeti_town_name());
		value.put(HETI_VICO_NAME, this.getHeti_vico_name());
		value.put(HETI_BUIL_UNIT, this.getHeti_buil_unit());
		value.put(HETI_BUIL_PER, this.getHeti_buil_per());
		value.put(HETI_DOCTOR, this.getHeti_doctor());
		value.put(HETI_BUIL_DATE, this.getHeti_buil_date());
		myContext.getContentResolver().insert(CONTENT_URI_TB1, value);
	}
	
	public void selectHereTipa(int pro){
		Cursor cursor = myContext.getContentResolver().query(CONTENT_URI_TB1, null,
				HETI_PROFILEID + "=" + pro + "", null, null);
		while(cursor.moveToNext()){
			this.heti_profileid=cursor.getInt(cursor.getColumnIndex(HETI_PROFILEID));
			this.heti_here_id = cursor.getString(cursor.getColumnIndex(HETI_HERE_ID));
			this.heti_name = cursor.getString(cursor.getColumnIndex(HETI_NAME));
			this.heti_now_addr = cursor.getString(cursor.getColumnIndex(HETI_NOW_ADDR));
			this.heti_hore_addr = cursor.getString(cursor.getColumnIndex(HETI_HORE_ADDR));
			this.heti_tel = cursor.getString(cursor.getColumnIndex(HETI_TEL));
			this.heti_town_name = cursor.getString(cursor.getColumnIndex(HETI_TOWN_NAME));
			this.heti_vico_name = cursor.getString(cursor.getColumnIndex(HETI_VICO_NAME));
			this.heti_buil_unit = cursor.getString(cursor.getColumnIndex(HETI_BUIL_UNIT));
			this.heti_buil_per = cursor.getString(cursor.getColumnIndex(HETI_BUIL_PER));
			this.heti_doctor = cursor.getString(cursor.getColumnIndex(HETI_DOCTOR));
			this.heti_buil_date = cursor.getString(cursor.getColumnIndex(HETI_BUIL_DATE));
			break;
		}
		cursor.close();
	}
	
	public void selectNameAndCode(int pro){
		Cursor cursor = myContext.getContentResolver().query(CONTENT_URI_TB1, PROJECTION,
				HETI_PROFILEID + "=" + pro + "", null, null);
		System.out.println("cursor.getCount="+cursor.getCount());
		while(cursor.moveToNext()){
			this.heti_here_id = cursor.getString(cursor.getColumnIndex(HETI_HERE_ID));
			this.heti_name = cursor.getString(cursor.getColumnIndex(HETI_NAME));
			break;
		}
		cursor.close();
	}
	
	public void updateHereTipa(int pro){
		ContentValues value;
		value = new ContentValues();
		value.put(HETI_PROFILEID, this.getHeti_profileid());
		value.put(HETI_HERE_ID, this.getHeti_here_id());
		value.put(HETI_NAME, this.getHeti_name());
		value.put(HETI_NOW_ADDR, this.getHeti_now_addr());
		value.put(HETI_HORE_ADDR, this.getHeti_hore_addr());
		value.put(HETI_TEL, this.getHeti_tel());
		value.put(HETI_TOWN_NAME, this.getHeti_town_name());
		value.put(HETI_VICO_NAME, this.getHeti_vico_name());
		value.put(HETI_BUIL_UNIT, this.getHeti_buil_unit());
		value.put(HETI_BUIL_PER, this.getHeti_buil_per());
		value.put(HETI_DOCTOR, this.getHeti_doctor());
		value.put(HETI_BUIL_DATE, this.getHeti_buil_date());
		myContext.getContentResolver().update(CONTENT_URI_TB1, value, 
				HETI_PROFILEID + "=" + pro + "", null);
	}
	
	
	public void delectHereTipa(int pro){
		myContext.getContentResolver().delete(CONTENT_URI_TB1, HETI_PROFILEID + "=" + pro + "", null);
	}
}
